package common.net
{
    import common.display.ActionImageData;

    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.system.LoaderContext;
    import flash.utils.ByteArray;

    import org.granite.net.AbstractLoader;
    import org.granite.net.DisplayAssetLoader;

    public class ActionDataLoader extends AbstractLoader
    {
        protected var _loader:Loader;
        private var _info:LoaderInfo;

        public function ActionDataLoader()
        {
            super ();
        }

        public function get actionImageData():ActionImageData
        {
            return _data as ActionImageData;
        }


        public function loadActionImageData(bytes:ByteArray , headLen:int):void
        {
            var actionImageData:ActionImageData = new ActionImageData ();
            _data = actionImageData;
            loadBytes (actionImageData.create (bytes , headLen));
        }

        private function loadBytes(bytes:ByteArray , context:LoaderContext = null):void
        {
            if (_isStarted)
            {
                throw new Error ('already started');
            }
            else
            {
                _isStarted = true;
            }
            _loader = new Loader ();
            _info = _loader.contentLoaderInfo;
            _info.addEventListener (Event.COMPLETE , _loadCompleteHandler);
            _info.addEventListener (IOErrorEvent.IO_ERROR , _loadIOErrorHandler);
            _info.addEventListener (ProgressEvent.PROGRESS , _loadProgressHandler);
            _loader.loadBytes (bytes , context);
        }

        override public function destroy(gc:Boolean = false):void
        {
            if (actionImageData)
            {
                actionImageData.destory ();
            }
            super.destroy ();
        }

        public override function close():void
        {
            if (_loader != null)
            {
                try
                {
                    _loader.close ();
                }
                catch (e:Error)
                {
                }
                _loader.unload ();
                super.close ();
            }
        }

        protected override function _loadIOErrorHandler(e:Event):void
        {
            _info.removeEventListener (Event.COMPLETE , _loadCompleteHandler);
            _info.removeEventListener (IOErrorEvent.IO_ERROR , _loadIOErrorHandler);
            _info.removeEventListener (ProgressEvent.PROGRESS , _loadProgressHandler);
            super._loadIOErrorHandler (e);
        }

        protected override function _loadCompleteHandler(e:Event):void
        {
            _info.removeEventListener (Event.COMPLETE , _loadCompleteHandler);
            _info.removeEventListener (IOErrorEvent.IO_ERROR , _loadIOErrorHandler);
            _info.removeEventListener (ProgressEvent.PROGRESS , _loadProgressHandler);
            var bmp:Bitmap = _loader.content as Bitmap;
            if (bmp == null)
            {
                _loadIOErrorHandler (new IOErrorEvent (IOErrorEvent.IO_ERROR , false , false , 'this asset is not a bitmapData'));
            }
            else
            {
                actionImageData.images = bmp.bitmapData;
                //stop();
                this.dispatchEvent (e);
            }
        }
    }
}
